package com.taoym.voucher.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taoym.voucher.common.enums.TextTypeEnum;
import com.taoym.voucher.admin.domain.XcxText;
import com.taoym.voucher.admin.entity.dto.XcxAddTextDTO;
import com.taoym.voucher.admin.entity.vo.XcxGetTextVO;
import com.taoym.voucher.admin.mapper.XcxTextMapper;
import com.taoym.voucher.admin.service.XcxTextService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @coder taoym
 * @datetime 2021/8/13 20:39
 * @desc
 */
@Service
public class XcxTextServiceImpl extends ServiceImpl<XcxTextMapper, XcxText> implements XcxTextService {

    @Resource
    private XcxTextMapper xcxTextMapper;

    @Override
    public Boolean addText(XcxAddTextDTO data) {

        XcxText xcxText = new XcxText();
        xcxText.setId(data.getId());
        xcxText.setText(JSON.toJSONString(data.getTexts()));
        xcxText.setType(data.getType());
        return saveOrUpdate(xcxText);
    }

    @Override
    public List<XcxGetTextVO> listTexts() {
        List<XcxText> xcxTexts = xcxTextMapper.selectList(new QueryWrapper<>());
        List<XcxGetTextVO> xcxGetTextVOS = new ArrayList<>(xcxTexts.size());
        xcxTexts.forEach(e -> {
            XcxGetTextVO xcxGetTextVO = new XcxGetTextVO();
            xcxGetTextVO.setId(e.getId());
            xcxGetTextVO.setTexts(JSON.parseArray(e.getText(), XcxGetTextVO.Value.class));
            xcxGetTextVO.setType(e.getType());
            xcxGetTextVO.setTypeDesc(TextTypeEnum.getInstance(e.getType()).getMsg());
            xcxGetTextVOS.add(xcxGetTextVO);
        });

        return xcxGetTextVOS;
    }

    @Override
    public Integer deleteText(Integer id) {
        return xcxTextMapper.deleteById(id);
    }
}
